package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.project.ThresholdManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.ThresholdManageRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ThresholdManageService {
    @Autowired
    private ThresholdManageRepository thresholdManageRepository;

    @Cacheable(value = "projectPage")
    public Page<ThresholdManage> findAllThreshold(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ThresholdManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ThresholdManage> page = thresholdManageRepository.findAll(mySpecifications, pageable);
        return page;

    }

    //保存
    public void saveThreshold(String id, String code, String name, Integer objName, String source, Integer isModify,
                              String sysDefVal, String unit) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        ThresholdManage tManage = new ThresholdManage();
        String uuid = "";
        if (id != null) {
            tManage = findById(id);
            tManage.setCreateUser(new User(shiroUser.getId()));
            tManage.setUpdateTime(new Date());
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            tManage.setId(uuid);
            tManage.setCreateTime(date);
            tManage.setCreateUser(new User(shiroUser.getId()));
            tManage.setIsValid(1);
        }

        tManage.setCode(code);
        tManage.setName(name);
        tManage.setObjName(objName);
        tManage.setSource(source);
        tManage.setIsModify(isModify);
        if (isModify == null) {
            isModify = 0;
        }
        tManage.setIsModify(isModify);
        tManage.setSysDefVal(sysDefVal);
        tManage.setUnit(unit);

        thresholdManageRepository.save(tManage);
    }

    // 通过id查找相应的信息
    public ThresholdManage findById(String id) {
        ThresholdManage code = thresholdManageRepository.findOne(id);
        return code;
    }

    // 删除
    public void delThresholdManage(String[] ids) {
        List<ThresholdManage> controlOrdersInfo = thresholdManageRepository.findAll(Arrays.asList(ids));
        for (ThresholdManage threshold : controlOrdersInfo) {
            threshold.setIsValid(0);
        }
        thresholdManageRepository.save(controlOrdersInfo);
    }

    //导出
    public List<ThresholdManage> findAllThresholdManageInfos() {
        return thresholdManageRepository.findThresholdManages();
    }

    //导入数据
    public String saveImport(List<String[]> list, String flag) {
        List<ThresholdManage> allCode = thresholdManageRepository.findCode();

        flag = "0";
        for (String[] info : list) {
            if (allCode.contains(info[0])) {
                return flag = "1";
        }
            ThresholdManage tinfo = new ThresholdManage();
            tinfo.setCode(info[0]);
            tinfo.setName(info[1]);
            tinfo.setObjName(Integer.valueOf(info[2]));
            tinfo.setSource(info[3]);
            tinfo.setIsModify(Integer.valueOf(info[4]));
            tinfo.setSysDefVal(info[5]);
            tinfo.setUnit(info[6]);


            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            tinfo.setId(uuid);
            tinfo.setCreateTime(new Date());
            tinfo.setCreateUser(new User(shiroUser.getId()));
            tinfo.setIsValid(1);
            thresholdManageRepository.save(tinfo);
        }
        return flag;
    }


    public List<ThresholdManage> findCode() {
        List<ThresholdManage> thminfo = thresholdManageRepository.findCode();
        return thminfo;
    }
}
